package com.hantanger.weblog.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.time.DateUtils;

import com.google.gdata.data.MediaContent;
import com.google.gdata.data.photos.PhotoEntry;
import com.hantanger.weblog.service.FileUploadService;
import com.hantanger.weblog.service.PicasaService;
import com.hantanger.weblog.util.Cache;
import com.hantanger.weblog.util.ClassFactory;

/**
 * 图片上传,含有type参数时把文件内容保存在cache中
 * @author lys
 *
 */
public class UploadController extends BaseController {
	
	private PicasaService getPicasaService(){
		return ClassFactory.getInstance(PicasaService.class);
	}

	@Override
	public String doGet(HttpServletRequest request, HttpServletResponse response) {
		request.setAttribute("isPhotoUplad", true);
		return "photoupload";
	}
	
	@Override
	public String doPost(HttpServletRequest request, HttpServletResponse response) {
		response.setContentType("text/html; charset=UTF-8");
		String type = request.getParameter("type");
		if(type != null){
			uploadFile(request, response);
			return null;
		}
		Map<String, byte[]> fileMap = FileUploadService.getFileUploadInfo(request);
		Map<String, String> data = new HashMap<String, String>();
		for(String title : fileMap.keySet()){
			byte[] bytes = fileMap.get(title);
			if(fileMap.get(title) != null && fileMap.get(title).length > 0){
				PhotoEntry photoEntry = getPicasaService().insertPhoto(bytes, title);
				if(photoEntry != null){
					data.put("title", title);
					data.put("url", ((MediaContent)photoEntry.getContent()).getUri());
					data.put("status", "SUCCESS");
					String state = "SUCCESS";
					String url = ((MediaContent)photoEntry.getContent()).getUri();
					try {
						PrintWriter writer = response.getWriter();
						writer.write("{'url':'"+ url +"','title':'"+title+"','state':'"+state+"'}");
						writer.flush();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				break;
			}
		}
		//renderJson(data, response,HTML);
		return null;
	}
	
	/**
	 * 上传文件并保存在memcached中
	 * @param request
	 * @param response
	 */
	private void uploadFile(HttpServletRequest request, HttpServletResponse response){
		String target = request.getParameter("target");//上传成功后跳转的目标地址
		Map<String, byte[]> fileMap = FileUploadService.getFileUploadInfo(request);
		if(fileMap != null && !fileMap.isEmpty()){
			String filePath = null;
			for(String fileName : fileMap.keySet()){
				String newName = System.currentTimeMillis() + fileName.substring(fileName.indexOf("."));
				String key = "filename_" + newName;
				Cache.put(key, fileMap.get(fileName), (int)(DateUtils.MILLIS_PER_DAY * 7 / 1000));
				filePath = getSpaceUrl() + "/file/" + newName;
				break;
			}
			if(target != null){
				redirect(target + "?" + filePath, request, response);
				return;
			}
			renderJson(filePath, response,HTML);
		}
		
	}
}
